import { toast, to, set, get } from './uniapi';

export default (url: string, data: any) => {
	const { envVersion } = uni.getAccountInfoSync().miniProgram;
	let baseUrl: string = '';
	if (envVersion === 'release') {
		baseUrl = 'https://prod-ymk-order.ymk.vin';
	} else {
		baseUrl = 'https://dev-ymk-order.ymk.vin';
	}
	let header = {};
	const openid = get('user').wxOpenId;
	if (openid) {
		header = { 'X-WX-OPENID': openid };
	}

	return new Promise((resolve, reject) => {
		uni.request({
			url: baseUrl + url,
			method: 'POST',
			data,
			header,
			success: (res: any) => {
				if (data.scope === 'mop' && res.data.respCode === 410) {
					toast(res.data.msg);
					setTimeout(() => {
						to('/pages/store/store');
					}, 1000);
				}
				if (data.scope === 'mop' && res.data.respCode === 200) {
					set('token', res.data.token);
				}
				resolve(res.data);
			},
			fail: (err) => {
				reject(err);
			}
		});
	});
};
